package com.chao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chao.entity.Department;
import com.chao.entity.Msg;
import com.chao.service.impl.DepartmentServiceImpl;
import com.chao.utils.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class DepartmentController {

    @Autowired
    DepartmentServiceImpl departmentService;


    /**
     * 批量删除和 单个删除
     */
    @PutMapping("/department/{dId}")
    @ResponseBody
    public Msg department_update(@Valid Department department,
                                 @PathVariable Integer dId) {
        department.setDId(dId);
        department.setDDate(new Date());
        departmentService.updateById(department);
        return Msg.success().add("msg", "更改成功");
    }


    @DeleteMapping("/department/{dIds}")
    @ResponseBody
    public Msg department_delete(@PathVariable String dIds) {
        if (dIds.contains("-")) {
            String[] ids = dIds.split("-");
            List<Integer> idList = TypeConversion.strArrToIntegerList(ids);
            departmentService.removeBatchByIds(idList);
        } else {
            departmentService.removeById(Integer.parseInt(dIds));
            return Msg.success().add("msg", "删除成功");
        }
        return Msg.success();
    }

    @PostMapping("/department")
    @ResponseBody
    public Msg department_add(@Valid Department department) {
        department.setDDate(new Date());
        departmentService.addOfPrimary(department);
        return Msg.success();
    }

    //查询所有
    @GetMapping("/department_all")
    @ResponseBody
    public Msg department_all(@RequestParam(defaultValue = "1") Integer pn) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        List<Department> departments = departmentService.getBaseMapper().selectList(queryWrapper);
        return Msg.success().add("departments", departments);
    }

    @GetMapping("/department")
    @ResponseBody
    public Msg department_table(@RequestParam(defaultValue = "1") Integer pn) {
        Page<Department> page = new Page<>(pn, 5);
        Page<Department> departmentPage = departmentService.page(page);
        List<Department> departments = departmentPage.getRecords();
        return Msg.success().add("departments", departments).add("page", page);
    }

    // 验证数据是否存在
    @PostMapping("/checkDept")
    @ResponseBody
    public Msg checkDept(@RequestParam String deptName) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("d_name", deptName);
        boolean exists = departmentService.getBaseMapper().exists(wrapper);
        if (exists) {
            return Msg.fail().add("va_msg", "院系名已存在");
        } else {
            return Msg.success();
        }
    }

    @GetMapping("/department_table")
    public String department_table() {
        return "department/department_table";
    }

}
